package cn.com.oyat.idea.service.impl;

import cn.com.oyat.idea.model.LicenseModel;
import cn.com.oyat.idea.service.LicenseService;
import cn.com.oyat.idea.util.idea.CertGenerator;
import cn.com.oyat.idea.util.idea.LicenseGenerator;
import cn.hutool.core.io.FileUtil;
import org.bouncycastle.asn1.*;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.Security;
import java.security.Signature;
import java.security.cert.X509Certificate;
import java.security.interfaces.RSAPublicKey;
import java.util.Arrays;
import java.util.Base64;

@Service
public class LicenseGeneratorService implements LicenseService {

    @Override
    public Boolean generateCert(LicenseModel licenseModel) {
        try {
            CertGenerator.createCert(licenseModel);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return true;
    }

    @Override
    public Boolean generateLicense(LicenseModel licenseModel) {
        try {
            LicenseGenerator.createLicense(licenseModel);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Boolean generateLicenseFile(LicenseModel licenseModel) {
        try {
            LicenseGenerator.generateLicenseFile(licenseModel);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
